﻿package app.model.proxy {	
	
    import app.facade.App;    
	import app.facade.EApp;
	import com.adobe.serialization.json.JSONDecoder;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import com.adobe.serialization.json.JSON;
	import com.hutra.utils.ArrayUtilities;
	import flash.system.Security;
    import org.puremvc.as3.interfaces.IProxy;
    import org.puremvc.as3.patterns.proxy.Proxy;
	import org.osflash.thunderbolt.Logger;
		
	/**
	 * @author hungconcon
	 */
    public class AssetProxy extends Proxy implements IProxy
    {
        public static const NAME:String = "AssetProxy";	
		
		public var list:Array;
		
		public function AssetProxy() {
			super(NAME);
        }
		override public function onRegister():void {
			super.onRegister();
		}
		public function load():void {
			var loader:URLLoader = new URLLoader();
				loader.addEventListener(Event.COMPLETE, completeHandler);
				loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            var request:URLRequest = new URLRequest(App.CONFIG_LINK);
            try { 
				loader.load(request); 
			} catch (error:Error) { 
				Logger.debug("Unable to load requested document.");
			}
        }
		/**
		 * [0] không nạp vào khi mới load game
		 * @param	event
		 */
		private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
			try {
				
				data = new JSONDecoder( loader.data, true ).getValue();//JSON.parse(loader.data);
				list = new Array();
				applyUrl(data.url);
				joinData(data.sound		, list);
				joinData(data.init		, list);
				joinData(data.app		, list, 1); // [0]
				applyUrl(data.app); // [0]
				
				sendNotification(App.LOAD_PROXY_DONE, NAME);
				sendNotification(App.LOADING_PROXY_ALL);
				
			}catch (ex:Error) {
				sendNotification(App.SHOW_ERROR,"AssetProxy.Error.JSON.standardless!");
			}
			
        }
        private function securityErrorHandler(event:SecurityErrorEvent):void {
			sendNotification(App.SHOW_ERROR,"AssetProxy.securityErrorHandler");
		}
        private function ioErrorHandler(event:IOErrorEvent):void { 
			sendNotification(App.SHOW_ERROR, "AssetProxy.ioErrorHandler");
		}
		private function applyUrl(a:Array):void {
			try {
				for (var i:int = 0; i < a.length; i++){
					var ob:Object = a[i] as Object;
					App.URL[ob.key] = ob.url;
				}
			}catch (ex:Error) {
				sendNotification(App.SHOW_ERROR, {value:"AssetProxy.applyUrl.Error"});
			}
		}
		/**
		 * @param	a is the last json array
		 * @param	b is the new array
		 * @param	isadd 0,1,2: 0 is only add to load, 1 is only add to App, 2 is add both
		 */
		private function joinData(a:Array, b:Array, isadd:int=0):void {
			for (var i:int = 0; i < a.length; i++) {
				var ob:Object = a[i] as Object;
				if (isadd == 0) {
					b.push(ob);
				}else if (isadd == 1) {
					App.URL[ob.key] = ob.url;
				}else if (isadd == 2) {
					App.URL[ob.key] = ob.url;
					b.push(ob);
				}
			}
		}
    }
}